/**
 * Created by Administrator on 2017/3/13.
 */
// console.log(-1%7)
var day = new Date().getDay();
function getDay(day) {
    day %= 7;
    switch (day){
        case 1:
            return "周一";
        case 2:
            return "周二";
        case 3:
            return "周三";
        case 4:
            return "周四";
        case 5:
            return "周五";
        case 6:
            return "周六";
        case 0:
            return "周日";
    }
}

$(function () {
    var $body = $("body");
    var navbar_h = $("nav").height();
    var footer_h = $("footer").height();
    // console.log(navbar_h+","+footer_h);
    $body.css("padding",navbar_h+20 +"px "+ 0 +" "+ (footer_h+30) +"px");
    // index中不需要设置body偏移
    $("#index").css("padding",0);

    //设置标题栏小图标
    $("head").append('<link rel="shortcut icon" href="img/smile.png">');

    // 弹出登录框
    $("#loginBtn").click(function () {
        $("body").append("<div id='loginDiv'></div>");
        $("#loginDiv").load("login.html");
    });

    //弹出注册框
    $body.on("click","#regBtn",function () {
        // $("#loginDiv").remove();
        $('#loginModal').modal("hide");
        $("body").append("<div id='regDiv'></div>");
        $("#regDiv").load("reg.html");
    });


    //表格在移动端中点击展开
    $("#articleManage").find(".more").click(function () {
        var tr = $(this).parents("tr");
        if(tr.next().attr("class") === "visible-xs"){
            $(this).text("更多");
            tr.nextAll().slice(0, 3).remove()
        }else{
            $(this).text("收起");
            var date = tr.find(".date").text();
            var views = tr.find(".views").text();
            var comments = tr.find(".comments").text();
            var content = '<tr class="visible-xs"><td colspan="2"><strong>发表日期：</strong>'+ date +'</td></tr>\
                        <tr class="visible-xs"><td colspan="2"><strong>浏览/评论：</strong>'+views+'/'+comments+'</td></tr>\
                        <tr class="visible-xs"><td colspan="2"><button class="btn btn-primary btn-sm">编辑</button>\
                                <button class="btn btn-warning btn-sm del">删除</button></td>\
                        </tr>';
            tr.after(content);
            $("tr.visible-xs").css("backgroundColor","rgba(60,100,200,.1)")
        }
    });

    //删除文章
    $("#articleManage").on("click",".del",function () {
        var r = confirm("您确认要删除吗？");
        if (r === true) {
            if($(this).parents("tr.visible-xs")[0]){
                $(this).parents("tr.visible-xs").prevAll().slice(0, 3).remove();
                $(this).parents("tr.visible-xs").remove();
            }else{
                $(this).parents("tr").remove()
            }

        }
    });

    //音乐天地中的列表播放
    $("#music").find("table .play").click(function () {
        var url = $(this).parent().next().find("a").attr("href");
        console.log(url)
        $("aside").find("audio").attr("src",url)[0].play();
    })
    // 背景音乐
    var musicAdr01 = "music/zyh_beautifulMinor.mp3";
    var musicAdr02 = "music/zyh_nightTell.mp3";
    var musicAdr03 = "music/zyh_theForgotten89thKey.mp3";
    var musicAdrRandom = randomArray(musicAdr01,musicAdr02,musicAdr03);
    $("#bgMusic").css("top",navbar_h+3).find("audio").attr("src",musicAdrRandom)
    // console.log(musicAdrRandom);
    var audio = document.getElementById("bgMusic").children[0];
    var audioPlay = audio.play();
    audioPlay.then(function () {
        console.log('可以自动播放');
    }).catch(function (err) {
        console.log(err);
        console.log("不允许自动播放");
        audio.pause();
        setTimeout(function () {
            if(confirm('背景音乐需要确认后播放')){
                audio.play();
            }else{
                audio.pause()
            }
        },500)

    })

});
function randomArray(n1,n2,n3) {
    var n = Math.random();
    if(n<.33){
        return n1;
    }else if(n<.66){
        return n2;
    }else{
        return n3;
    }

}